﻿layui.config({
    base: '/Lib/layui/extend/'
}).extend({
    formSelects: 'formSelects-v4'
}).use(['form', 'layedit', 'layer', 'table', 'jquery', 'formSelects'], function () {
    var form = layui.form, layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        player = parent.layer || layui.layer
    formSelects = layui.formSelects; //很重要;

    var staffEmployeeEntryModifyLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                projectAllList: "/Project/Info/LoadProjectAllList",


                saveUrl: "/Staff/EmployeeEntry/ModifyInfo",
                uploadImgUrl: "/Staff/EmployeeEntry/UploadImg",
                loadImgUrl: "/Staff/EmployeeEntry/LoadEmployeeImg",
               
            };

            this.initPage();

            this.bindEvent();

        },
        //初始化列表
        initPage: function () {

            self.logicFunc.loadProjectAllList(function () {

                //获取信息
                var data = GrdAssist.queryString("data");
                self.logicFunc.loadInfo(data);

                self.logicFunc.loadImg(data);

                form.render();
            });
        },
        //绑定事件
        bindEvent: function () {

            //上传图片
            $(".userFaceBtn").on("click", this.logicFunc.uploadImg);

            //监听提交
            form.on('submit(modifyButton)', self.logicFunc.saveInfo);

            //关闭
            $("#btnClose").on("click", self.logicFunc.close);

        },
        //逻辑方法
        logicFunc: {

            //获取所有的工程信息
            loadProjectAllList: function (func) {
                GrdLayUI.ajaxPost({
                    url: self.Parameters.projectAllList,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            $("#ProjectNo").empty();
                            for (var i = 0; i < result.Data.length; i++) {
                                $("#ProjectNo").append("<option value=" + result.Data[i].ProjectNo + ">" + result.Data[i].ProjectName + "</option>");
                            }
                        }
                        if (func) {
                            func();
                        }
                    }
                });
            },

            //获取修改的信息
            loadInfo: function (data) {
                GrdLayUI.assignmentData("#modify-form", data);
                var jsonData = GrdLayUI.toJsonData(data);
                //$("#Sex").val(jsonData.Sex.trim());
                //$("#ProjectNo").val(jsonData.ProjectNo.trim());
            },

            //加载图片
            loadImg: function (data) {
                var jsonData = GrdLayUI.toJsonData(data);
                var json = {
                    parameters: jsonData.UserNo
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.loadImgUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {
                            $("#userFace").attr("src", result.Data.FilePath); //头像
                        }
                    }
                });
            },

            //上传图片
            uploadImg: function (obj) {

                var callback_name = "loadUploadImgPage_callback_" + new Date().getTime();

                window[callback_name] = function (name, fullUrl) {

                    $(".userFaceBtn").attr({ src: fullUrl + "?v=" + Math.random(), "data-savesrc": fullUrl });
                };

                var index = layui.layer.open({
                    title: "图片上传",
                    type: 2,
                    resize: false,
                    area: ['820px', '650px'],
                    content: self.Parameters.uploadImgUrl + '?' + $.param({
                        imageType: "EmployeeInfo",
                        callback: callback_name,
                        width: 280,
                        height: 340
                    }),
                    end: function () {
                        delete window[callback_name];
                    }
                });
            },

            //保存数据
            saveInfo: function () {

                var json = {
                    parameters: GrdLayUI.initParamsData($("#modify-form"))
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.saveUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {

                            GrdLayUI.msg(result.Msg, "成功", function () {
                                //获取窗口索引
                                var index = player.getFrameIndex(window.name);
                                //关闭子页面
                                player.close(index);
                                //刷新父页面
                                window.parent.location.reload();
                            });
                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },

            //取消
            close: function () {
                player.closeAll();
            }
        },
        //工具
        tools: {
        }
    };
    staffEmployeeEntryModifyLogic.init();
});
